﻿Imports System.Windows.Threading
Imports FuncClasses
Imports HtmlAgilityPack
Imports System.Net
Imports System.Collections.ObjectModel

Public Class DHL
    Implements IPackager

    Private _id As String
    WithEvents uicontrol As New DHLControl

    Public Sub New(ByVal ID As String)
        _id = ID
        If String.IsNullOrEmpty(ID) = False Then
            Try : RefreshState() : Catch ex As Exception : End Try
        End If
    End Sub

    Public Property Package_ID As String Implements IPackager.Package_ID
        Get
            Return _id
        End Get
        Set(value As String)
            _id = value
            Try : RefreshState() : Catch ex As Exception : End Try
        End Set
    End Property

    Public Sub RefreshState() Implements IPackager.RefreshState
        GetAllStates()
    End Sub

    Public ReadOnly Property InfoControl As System.Windows.UIElement Implements IPackager.InfoControl
        Get
            Return uicontrol
        End Get
    End Property

#Region "GetAllStates"
    WithEvents dclient2 As New Net.WebClient()
    WithEvents timer2 As New DispatcherTimer
    Dim details As New ObservableCollection(Of KeyValuePairs)

    Public Sub GetAllStates() Implements IPackager.GetAllStates
        details.Clear()
        infolaststate = Nothing
        bImages.Clear()
        dclient2.Encoding = Text.Encoding.UTF8
        dclient2.DownloadStringAsync(New Uri("http://nolp.dhl.de/nextt-online-public/set_identcodes.do?lang=de&idc=" + Package_ID))
        timer2.Interval = New TimeSpan(0, 0, 0, 0, 5)
        timer2.Start()
    End Sub

    Private Sub CompletedDetails(sender As Object, e As DownloadStringCompletedEventArgs) Handles dclient2.DownloadStringCompleted
        Try
            '### Analyse des Results ###'
            Dim doc As New HtmlDocument
            doc.LoadHtml(e.Result)
            Dim link As HtmlNode
            link = doc.DocumentNode.SelectSingleNode("/html[1]/body[1]/div[6]/div[1]/div[2]/span[1]/div[1]/div[1]/table[1]/tbody[1]/tr/td/div/table/tbody") ' Tabelle mit mehr Infos
            For Each item As HtmlNode In link.ChildNodes
                Dim list As New Collection(Of String)
                If item.Name = "tr" Then
                    For Each childitem As HtmlNode In item.ChildNodes
                        If childitem.Name = "td" Then
                            list.Add(childitem.InnerText.Trim)
                        End If
                    Next
                    list(0) = list(0).Replace("Uhr", "").Trim + " Uhr"
                    details.Add(New KeyValuePairs(list(0), list(1), list(2)))
                End If
            Next

            link = doc.DocumentNode.SelectSingleNode("/html[1]/body[1]/div[6]/div[1]/div[2]/span[1]/div[1]/div[1]/table[1]/tbody[1]/tr[2]/td[3]") 'Produkt
            Dim produkt As String = link.InnerText.Trim

            link = doc.DocumentNode.SelectSingleNode("/html[1]/body[1]/div[6]/div[1]/div[2]/span[1]/div[1]/div[1]/table[1]/tbody[1]/tr[3]/td[3]") 'Letzte Meldung
            Dim letzterstatus As String = link.InnerText.Trim

            Dim empfaenger As String
            Try
                link = doc.DocumentNode.SelectSingleNode("/html[1]/body[1]/div[6]/div[1]/div[2]/span[1]/div[1]/div[1]/table[1]/tbody[1]/tr[5]/td[3]") ' Zugestellt an
                empfaenger = link.InnerText.Trim
            Catch ex As Exception
                'Do Nothing. Package not delivered yet
                empfaenger = ""
            End Try

            bImages.Add(e.Result.Contains("Icon_01_SendungErhaltenACTIVE_58x58.gif") OrElse e.Result.Contains("Icon_01_FilialeACTIVE_58x58.gif"))
            bImages.Add(e.Result.Contains("Icon_02_TransportACTIVE_58x58.gif"))
            bImages.Add(e.Result.Contains("Icon_03_PaketzentrumACTIVE_58x58.gif"))
            bImages.Add(e.Result.Contains("Icon_04_ZustellungACTIVE_58x58.gif"))
            bImages.Add(e.Result.Contains("Icon_05_ZugestelltACTIVE_58x58.gif"))

            '### Produkt - Letzter Status - Empfänger
            infolaststate = New KeyValuePairs(produkt, letzterstatus, empfaenger)
        Catch ex As Exception : End Try
    End Sub

    Private Sub Timer2_Tick(sender As Object, e As EventArgs) Handles timer2.Tick
        If infolaststate IsNot Nothing Then
            timer2.Stop()
            uicontrol.SetImages(bImages(0), bImages(1), bImages(2), bImages(3), bImages(4))
            uicontrol.SetLastState(infolaststate)
            uicontrol.SetDetails(details)
        End If
    End Sub
#End Region

#Region "Letzter Status"
    WithEvents dclient As New Net.WebClient()
    WithEvents timer1 As New DispatcherTimer
    Dim infolaststate As KeyValuePairs
    Dim bImages As New Collection(Of Boolean)

    Public Sub GetLastState() Implements IPackager.GetLastState
        infolaststate = Nothing
        bImages.Clear()
        dclient2.Encoding = Text.Encoding.UTF8
        dclient.DownloadStringAsync(New Uri("http://nolp.dhl.de/nextt-online-public/set_identcodes.do?lang=de&idc=" + Package_ID))
        timer1.Interval = New TimeSpan(0, 0, 0, 0, 5)
        timer1.Start()
    End Sub

    Private Sub CompletedLastState(sender As Object, e As DownloadStringCompletedEventArgs) Handles dclient.DownloadStringCompleted
        Try
            '### Analyse des Results ###'
            Dim doc As New HtmlDocument
            doc.LoadHtml(e.Result)
            Dim link As HtmlNode

            link = doc.DocumentNode.SelectSingleNode("/html[1]/body[1]/div[6]/div[1]/div[2]/span[1]/div[1]/div[1]/table[1]/tbody[1]/tr[2]/td[3]") 'Produkt
            Dim produkt As String = link.InnerText.Trim

            link = doc.DocumentNode.SelectSingleNode("/html[1]/body[1]/div[6]/div[1]/div[2]/span[1]/div[1]/div[1]/table[1]/tbody[1]/tr[3]/td[3]") 'Letzte Meldung
            Dim letzterstatus As String = link.InnerText.Trim

            Dim empfaenger As String
            Try
                link = doc.DocumentNode.SelectSingleNode("/html[1]/body[1]/div[6]/div[1]/div[2]/span[1]/div[1]/div[1]/table[1]/tbody[1]/tr[5]/td[3]") ' Zugestellt an
                empfaenger = link.InnerText.Trim
            Catch ex As Exception
                'Do Nothing. Package not delivered yet
                empfaenger = ""
            End Try

            bImages.Add(e.Result.Contains("Icon_01_SendungErhaltenACTIVE_58x58.gif") OrElse e.Result.Contains("Icon_01_FilialeACTIVE_58x58.gif"))
            bImages.Add(e.Result.Contains("Icon_02_TransportACTIVE_58x58.gif"))
            bImages.Add(e.Result.Contains("Icon_03_PaketzentrumACTIVE_58x58.gif"))
            bImages.Add(e.Result.Contains("Icon_04_ZustellungACTIVE_58x58.gif"))
            bImages.Add(e.Result.Contains("Icon_05_ZugestelltACTIVE_58x58.gif"))

            '### Produkt - Letzter Status - Empfänger
            infolaststate = New KeyValuePairs(produkt, letzterstatus, empfaenger)
        Catch ex As Exception

        End Try
    End Sub

    Private Sub Timer1_Tick(sender As Object, e As EventArgs) Handles timer1.Tick
        If infolaststate IsNot Nothing Then
            timer1.Stop()
            uicontrol.SetLastState(infolaststate)
            uicontrol.SetImages(bImages(0), bImages(1), bImages(2), bImages(3), bImages(4))
        End If
    End Sub
#End Region

    Public ReadOnly Property Details1 As ObservableCollection(Of FuncClasses.KeyValuePairs) Implements IPackager.Details
        Get
            Return details
        End Get
    End Property

    Public ReadOnly Property LastState As String Implements IPackager.LastState
        Get
            Return infolaststate.Value
        End Get
    End Property

    Public ReadOnly Property Packager As Packager Implements IPackager.Packager
        Get
            Return PacketTracking.Packager.DHL
        End Get
    End Property

    Private Sub Refresh() Handles uicontrol.RefreshRequested
        GetAllStates()
    End Sub
End Class
